package de.tyde.mylazysms;

import android.accounts.Account;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DBhelper{
	final static String DB_NAME = "MyLazySMSDatabase";
	final static String TABLE_NAME = "MyLazySMSTable_User";
	private static final int DATABASE_VERSION = 2;
	private static final String TAG = "dbAdapter";
	
	private DatabaseHelper mDbHelper;
	private SQLiteDatabase mDb;
	private final Context mCtx;
	private static class DatabaseHelper extends SQLiteOpenHelper {

		DatabaseHelper(Context context) {
			super(context, DB_NAME, null, DATABASE_VERSION);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {

		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
					+ newVersion + ", which will destroy all old data");
			db.execSQL("DROP TABLE IF EXISTS entry");
			onCreate(db);
		}
	}


	public DBhelper(Context ctx) {
		this.mCtx = ctx;
	}

	public DBhelper open() throws SQLException {
		mDbHelper = new DatabaseHelper(mCtx);
		mDb = mDbHelper.getWritableDatabase();

		return this;
	}

	public void close() {
		mDbHelper.close();
	}


	public Cursor getUser() {

		return mDb.query(TABLE_NAME, new String[] { "_id", "user", "pw" }, null, null, null, null, null);
	}
	public void insertUser(MyAccount account){
		mDb.execSQL("INSERT INTO " + TABLE_NAME + " (user, pw) VALUES ('"
				+ account.account.name + "','"+account.pw+"');");
	}

	
}